# -*- coding: utf-8 -*-
import scrapy
import re
from scrapy.http import Request
from dangdangSpider.items import DangdangspiderItem

class DangdangSpider(scrapy.Spider):
    name = 'dangdang'
    allowed_domins = ['dangdang.com']
    start_urls = ['http://category.dangdang.com/pg1-cp01.54.06.00.00.00.html']

    def parse(self, response):
        # item = DangdangspiderItem()
        # item['link'] = response.xpath('//a[@class="img"]/@href').extract()
        # yield item
        url_list = response.xpath('//a[@class="pic"]/@href').extract()
        for url in url_list:
            yield Request(url,callback = self.parse_name)
        for i in range(2,5):
            page_url = 'http://category.dangdang.com/pg{}-cp01.54.06.00.00.00.html'.format(i)
            yield Request(page_url,callback=self.parse)
    def parse_name(self,response):
        items = DangdangspiderItem()
        items['title'] = response.xpath('//div[@class="name_info"]/h1/@title').extract()
        items['num'] = response.xpath('//a[@id="comm_num_down"]/text()').extract()
        p = response.xpath('//*[@id="dd-price"]').extract()
        items['price'] = re.search('\d+(\.\d+)?',str(p)).group()


        yield items
